﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simple.Common.Helpers
{
    public static class EnumHelper
    {
        public static string GetDescriptionContent(this Enum en)
        {
            if (en == null) return string.Empty;
            var type = en.GetType();   //获取类型  
            var memberInfos = type.GetMember(en.ToString());   //获取成员  
            if (memberInfos != null && memberInfos.Length > 0)
            {
                //获取特性  
                if (memberInfos[0].GetCustomAttributes(typeof(DescriptionAttribute), false)
                 is DescriptionAttribute[] attrs && attrs.Length > 0)
                {
                    //return attrs[0].Name ?? attrs[0].Description;    返回当前名称  
                    return attrs[0].Description;
                }
            }
            return en.ToString();
        }
    }
}
